<template>
  <div class="box-bg-color">
    <div class="filter-container">
      <div class="filter-item">
        <el-select v-model="searchReq.status" clearable style="width: 100px" placeholder="请选择">
          <el-option label="成功" :value="1">
            <k-badge type="primary" content="成功"></k-badge>
          </el-option>
          <el-option label="失败" :value="0">
            <k-badge type="danger" content="失败"></k-badge>
          </el-option>
        </el-select>
      </div>
      <div class="filter-item">
        <el-input v-model="searchReq.operaName" placeholder="操作人员" style="width: 200px" clearable></el-input>
      </div>
      <div class="filter-item">
        <el-input v-model="searchReq.operaIp" placeholder="操作IP" style="width: 200px" clearable></el-input>
      </div>
      <div class="filter-item">
        <el-button type="primary" @click="searchEvent">搜索</el-button>
      </div>
    </div>

    <k-table
      ref="tableRef"
      v-bind="tableData"
      :callback="getOperalogListApi"
      :loading="loading"
      stripe
      current-row-key="id"
    >
      <template #logo="{ row }">
        <div class="user-account-wrap" style="justify-content: center;">
          <el-avatar :src="row.logo" shape="square" :size="40"></el-avatar>
        </div>
      </template>
      <template #status="{ row }">
        <k-badge
          :type="row.status === 1 ? 'primary' : 'danger'"
          :content="row.status === 1 ? '成功' : '失败'"
        ></k-badge>
      </template>
    </k-table>

  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, type UploadRawFile } from 'element-plus'
import type { IKTableProps } from 'k-ui'

import appConfig from '@/config/index'
import { getToken } from '@/utils/cache'
import { confirmElBox, dateStrFormat, downLoad } from '@/utils'

import {
  getOperalogList,
  type QueryOperalogList,
  type OperalogApiResult
} from '@/api/operalog'

import type { ListResultData, Pagination } from '@/api/base'
import { hasPerms } from '@/utils/perm'
import { useApiLock } from '_hooks'

const tableRef = ref()
const tableData = ref<IKTableProps<OperalogApiResult>>({
  mode: 'config',
  data: { list: [], total: 0 },
  auto: true,
  isPager: true,
  pageSize: 20,
  index: true,
  columns: [
    { label: '系统菜单', prop: 'systemMenu', slot: true },
    { label: '操作模块', prop: 'operaModule', default: '--' },
    { label: '操作人员', prop: 'operaName', default: '--', width: 120 },
    { label: '操作ip', prop: 'operaIp', default: '--' },
    { label: '状态', prop: 'status', slot: true, width: 100 },
    {
      label: '操作时间',
      prop: 'createDate',
      formatter: (row: OperalogApiResult) => dateStrFormat(row.createDate as string),
      width: 100
    }
  ]
})

const loading = ref<boolean>(false)

const searchReq = ref<QueryOperalogList>({
  page: 1,
  size: 20,
  status: '',
  operaName: '',
  operaIp: '',
})
// 查询表格事件
const queryReq = ref<QueryOperalogList>({ page: 1, size: 20 })

const getOperalogListApi = async ({ page, size }: Pagination) => {
  loading.value = true
  const res = await useApiLock(() => getOperalogList({ ...queryReq.value, page, size } as QueryOperalogList), 500)
  loading.value = false
  if (res?.code === 200) {
    const data = res.data as ListResultData<OperalogApiResult>
    tableData.value.data = data
  } else {
    ElMessage({ message: res?.msg || '网络异常，请稍后重试', type: 'error' })
  }
}

const updateOperalogSuccess = (newPage = {}) => {
  // 在当前页 重新加载数据
  tableRef.value.refreshData(newPage)
}

const searchEvent = () => {
  queryReq.value = Object.assign({}, searchReq.value)
  updateOperalogSuccess({ page: 1, size: 10 })
}

</script>

<style lang="scss" scoped>
.user-account-wrap {
  display: flex;
  align-items: center;
  padding: 0 10px;
}

.upload-wrap {
  display: flex;
}
</style>
